# Create your views here.

from django.http import HttpResponse,FileResponse
from os import path

from .factory import start


def index(request):
    return HttpResponse("This is homePage")


def upload(request):
    if request.is_ajax():
        f_obj = request.FILES.get("files")
        f = open(path.join('./startfiles',f_obj.name),'wb')
        for chunk in f_obj.chunks():
            f.write(chunk)
        f.close()
    id = 1
    start.start(id)
    response = HttpResponse()
    response["Access-Control-Allow-Origin"] = "*"
    return response


def download(request):
    filename = "new.pptx"
    id = 1
    p = path.dirname(path.realpath(__file__))+'\middle\\'+str(id)+'\\new.pptx'
    print(p)
    f = open(p,'rb')
    if f: # and a new pptx file produced just now
        response = FileResponse(f)
        response['Mime-Type'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
        response['Content-Disposition'] = 'attachment;filename=' + filename
        response["Access-Control-Allow-Origin"] = "*"
        return response

